#include <gtest/gtest.h>


#include "memdb/memdb.h"
#include "memdb/impl/mock/MockDB.h"
#include "memdb/impl/assistant/Assistant.h"

TEST(MemdbTest, testDB){
  memdb::impl::mock::MockDB db;
  if(db.open()){

    auto & tables = db.tables();
    ASSERT_FALSE(tables.drop("table_1"));

    { //create success
      auto r = tables.create("table_1");
      ASSERT_TRUE(r.first);
      r.second->insert("key", "value");
    }

    { //table already exist
      auto r = tables.create("table_1");
      ASSERT_FALSE(r.first);
      std::cerr << "DBError:" << "table already exists" << std::endl;
    }

    auto tab_get = tables.get("table_1");
    ASSERT_TRUE(tab_get);
    tab_get.value()->insert("key", "value");


    db.close();
  }
  db.destroy();
}

TEST(MemdbTest, testPreData){
  using Assistant = memdb::impl::assistant::Assistant;
  memdb::impl::mock::MockDB db;
  if(db.open()){

    auto & tables = db.tables();

    { //create success
      auto r = tables.create("table_1");
      ASSERT_TRUE(r.first);

      memdb::ITable * table = r.second;
      ASSERT_TRUE(Assistant::fillData(table));
      ASSERT_EQ(100u, table->count());

      std::string value = "";
      ASSERT_TRUE(table->get("00000004", value));
      ASSERT_EQ("{\"orderNo\":\"00000004\",\"offsetFlag\":\"1\"}", value);
    }
  }
 
}
